<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem" />
  </div>
  <fileImport v-model:importVisible="importVisible" :importItem="importItem" @onSuccess="onSuccess" @onError="onError" />
<!--禁用/启用/删除原因-->
  <el-dialog v-model="dialog.visible" :title="dialog.title" width="600px" @close="closeDialogHandle">
    <el-form :rules="rules" ref="ruleFormRef" :model="dialog.dataForm" label-width="80px">
      <el-form-item label="原因" prop="reason" >
        <el-input style="width: 80%" type="textarea" v-model="dialog.dataForm.reason" placeholder="请输入原因" />
      </el-form-item>
    </el-form>
    <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialog.visible = false">取 消</el-button>
                    <el-button type="primary" @click="confirmDialogHandle"> 确 定</el-button>
                </span>
    </template>
  </el-dialog>
</template>
<script setup>
import tablePaging from '@/views/common/component/table-paging'
import fileImport from '@/views/common/component/fileImport'
import {onMounted, reactive, ref} from 'vue'
import {operationItem, tableItem} from './const.js'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'
import {delStudent, disableStudent, resetStudentPassword} from '@/api/student'
import {listDictItemByCode} from '@/api/config/dic'

const { proxy } = getCurrentInstance();
const router = useRouter()
const ruleFormRef = ref('')
const dialog = reactive({
  code: '',
  visible: false,
  title: '',
  dataForm: {
    id: '',
    idList: [],
    disableFlag: '',
    reason: '',
  }
})
const rules = reactive({
  reason: [
    { required: true, message: '请输入原因', trigger: 'blur' },
  ],
})
const importVisible = ref(false)
const importItem = reactive({
  downUrl: '/admin/customer/user/download',//模板下载地址
  upUrl: '/admin/customer/user/import',//文件上传地址
  blob: true,//二进制文件下载
  fileDown() {
  },//自定义文件下载方法 如果传入下载地址会失效 如果都不传会返回一个fileDown函数 自己@fileDown接收
  downTitle: '',//下载标题 默认1、模板下载
  downButtonText: '',//下载按钮 下载
  upButtonText: '',//上传按钮 上传
  upTitle: '',//上传标题 默认2、表格上传
  downName: '学员模版'
})
const onSuccess = (file) => {
  tableItem.pageInfo.paging()
}
const onError = (file) => { }
function importHandle() {
  importVisible.value = true
}
function exportHandle() {
  proxy.download("/admin/customer/user/export", tableItem.pageInfo.request, `学员列表.xlsx`);
}
function closeDialogHandle() {
  dialog.visible = false
  dialog.dataForm.id = ''
  dialog.dataForm.idList = []
  dialog.dataForm.reason = ''
  dialog.dataForm.disableFlag = ''
}
function confirmDialogHandle() {
  if (!ruleFormRef.value) return
   ruleFormRef.value.validate((valid, fields) => {
    if(!valid){ return }
    if(dialog.code == 'disable') {
      disableStudent(dialog.dataForm).then(res => {
        closeDialogHandle()
        ElMessage.success('操作成功')
        tableItem.pageInfo.paging()
      })
    } else if(dialog.code == 'del'){
      delStudent(dialog.dataForm).then(res => {
        closeDialogHandle()
        ElMessage.success('操作成功');
        tableItem.pageInfo.paging()
      })
    }
  })
}
function initTableItem() {
  // 重置
  operationItem.menuArray[0].handle = () => {
    resetSearch()
  }
// 查询
  operationItem.menuArray[1].handle = () => {
    search()
  }
// 批量删除
  operationItem.menuArray[2].handle = () => {
    batchDelHandle()
  }
// 导入
  operationItem.menuArray[3].handle = () => {
    importHandle()
  }
// 导出
  operationItem.menuArray[4].handle = () => {
    exportHandle()
  }
//详情
  tableItem.btnArray[0].handle = (row) => {
    goDetail(row)
  }
//修改
  tableItem.btnArray[1].handle = (row) => {
    editHandle(row)
  }
//启用
  tableItem.btnArray[2].handle = (row) => {
    disableHandle(row)
  }
//禁用
  tableItem.btnArray[3].handle = (row) => {
    disableHandle(row)
  }
//重置密码
  tableItem.btnArray[4].handle = (row) => {
    resetPwdHandle(row)
  }
}
function initRequest() {
  tableItem.pageInfo.request = {
    'name': '', //用户名
    'mobile': '', //手机号
    'intentionalExamId': '',
    'addWxFlag': '', //是否添加微信
    'hasOrderFlag': '', //是否下单过1-是0-否
    'registerStartTime': '', //注册时间开始范围
    'registerEndTime': '', //注册时间结束范围
    'pageDomain': {
      'pageNum': '1',//分页页面数
      'pageSize': '10',//分页页面数据大小
      'orderByColumn': '',//搜索参数
      'isAsc': '',//搜索参数
      'reasonable': ''//搜索参数
    }
  }
}
function setIntentionalExamOptions() {
  operationItem.SearchItem[4].options = []
  listDictItemByCode({typeCode: 'courseTypeCode'}).then(res => {
    const courseList = res.rows
    courseList.map(item => {
      operationItem.SearchItem[4].options.push({ label: item.name, value: item.id })
    })
  })

}

function search() {
  tableItem.pageInfo.paging()
}
function resetSearch() {
  initRequest()
  search()
}
function goDetail(row) {
  router.push({
    path: '/modules/student/student/detail',
    query: { id: row.id}
  })
}
function editHandle(row) {

}
function disableHandle(row) {
  dialog.code = 'disable'
  dialog.visible = true
  dialog.dataForm.id = row.id
  if(row.disableFlag == 1) {
    dialog.dataForm.disableFlag = 0
    dialog.title = '启用'
  } else {
    dialog.dataForm.disableFlag = 1
    dialog.title = '禁用'
  }
}

const selectedRow = ref([])
tableItem.selectChange = (row) => {
  //构建需要删除的id列表
  selectedRow.value = row.map( (item) => {
    return item.id
  } )
}
//批量删除
function batchDelHandle() {
  if(selectedRow.value.length < 1) {return ElMessage.warning('请先选择学员');}
  dialog.code = 'del'
  dialog.visible = true
  dialog.title = '批量删除'
  dialog.dataForm.idList = [...selectedRow.value]
}
//重置密码
function resetPwdHandle(row) {
  proxy.$modal.confirm('密码重置后新密码为账号后六位，请确认是否要重置?').then(function () {
    return resetStudentPassword(row.id);
  }).then(res => {
    ElMessage.success(res.msg);
  }).catch(() => { });
}

onMounted(() => {
  initTableItem()
  initRequest()
  setIntentionalExamOptions()
})
</script>

<style></style>
